package com.example.jm24;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import pl.droidsonroids.gif.GifImageView;


public class LoginActivity extends AppCompatActivity {
    //提示框
    private ProgressDialog dialog;
    private static  final  String TAG="MainActivity";
    private EditText username;
    private EditText password;
    private Button register;
    private Button login;
    private CheckBox save;
    GifImageView view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        findViews();
        initWindow();//初始化，将状态栏和标题栏设为透明
        SharedPreferences sp;
        login.setOnClickListener(new View.OnClickListener() {
                                     public void onClick(View v) {
                                         switch (v.getId()) {//判断是否按下Checkbox
                                             case R.id.login_login:
                                                 if (save.isChecked()) {//保存密码                   //当多选按钮按下时执行报村数据
                                                     saveInfo();
                                                 } else {
                                                     clearInfo();
                                                 }
                                                 dialog = new ProgressDialog(LoginActivity.this);
                                                 dialog.setTitle("正在登录");
                                                 dialog.setMessage("请稍后");
                                                 dialog.setCancelable(false);//设置可以通过back键取消
                                                 dialog.show();
                                         }
                                         new Thread(new Runnable() {
                                             @Override
                                             public void run() {
                                                 String n = username.getText().toString().trim();
                                                 String psw = password.getText().toString().trim();
                                                 if (n.equals("")||psw.equals("")){
                                                     Looper.prepare();
                                                     Toast toast = Toast.makeText(LoginActivity.this,"输入不能为空！",Toast.LENGTH_SHORT);
                                                     toast.show();
                                                     dialog.dismiss();
                                                     Looper.loop();
                                                 }
                                                 UserDao ud = new UserDao();
                                                 Boolean result = ud.login(n,psw);
                                                 if (!result){
                                                     Looper.prepare();
                                                     Toast toast=Toast.makeText(LoginActivity.this,"用户名不存在或密码错误！",Toast.LENGTH_SHORT);
                                                     toast.show();
                                                     dialog.dismiss();
                                                     Looper.loop();
                                                 }else{
                                                     UserDao u = new UserDao();
                                                     String url=u.GetUser(n).getUrl();
                                                     Looper.prepare();
                                                     Toast toast=Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT);
                                                     toast.show();
                                                     dialog.dismiss();
                                                     //一下代码为跳转界面
                                                      Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                                                     intent.putExtra("username",n);
                                                     intent.putExtra("url",url);
                                                      startActivity(intent);
                                                     Looper.loop();

                                                 }

                                                 //以上为jdbc登录
                                             }
                                         }).start();

                                     }

                                 });


        /*username = findViewById(R.id.login_accout);
        password = findViewById(R.id.login_password);
        register = (Button) findViewById(R.id.login_register);
        save = (CheckBox) findViewById(R.id.remember_password);
        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });*/
        /*
        Android 五种数据存储的方式分别为：

        SharedPreferences：以Map形式存放简单的配置参数；SharedPreferences是Android平台上一个轻量级的存储类，用来保存应用的一些常用配置，比如Activity状态，Activity暂停时，将此activity的状态保存到SharedPereferences中；当Activity重载，系统回调方法onSaveInstanceState时，再从SharedPreferences中将值取出。
        ContentProvider：将应用的私有数据提供给其他应用使用；
        文件存储：以IO流形式存放，可分为手机内部和手机外部（sd卡等）存储，可存放较大数据；
        SQLite：轻量级、跨平台数据库，将所有数据都是存放在手机上的单一文件内，占用内存小；
        网络存储 ：数据存储在服务器上，通过连接网络获取数据
         */
        sp = getSharedPreferences("user.xml", MODE_PRIVATE);//获得SharesPreferences对象（参数为（文件名，存贮模式））
        if (sp.getBoolean("save", false) == true) {    //判断是否写入了数值save==true
            getInfo();
        }
    }
    private void findViews() {
        username = (EditText) findViewById(R.id.login_accout);
        username.getBackground().setAlpha(150);//0~255透明度值
        password = (EditText) findViewById(R.id.login_password);
        login = (Button) findViewById(R.id.login_login);
        login.getBackground().setAlpha(180);//0~255透明度值
        register = (Button) findViewById(R.id.login_register);
        register.getBackground().setAlpha(180);//0~255透明度值
        save = (CheckBox) findViewById(R.id.remember_password);
        View v = findViewById(R.id.back);//找到你要设透明背景的layout 的id
        v.getBackground().setAlpha(220);//0~255透明度值
        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
    //清除
    private void clearInfo() {
        SharedPreferences sp = getSharedPreferences("user.xml", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();//获得Editor对象
        editor.clear();//清楚数据//清除SharedPreferences数据一定要执行editor.clear()，editor.commit()，不能只是简单的删除文件，
        editor.commit();//提交
    }
    //保存数据
    private void saveInfo() {
        SharedPreferences sp = getSharedPreferences("user.xml", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username", username.getText().toString());
        editor.putString("password", password.getText().toString());//通过editor对象写入数据//以键值对形式存储
        editor.putBoolean("save", true);
        editor.commit();            //提交写入数据
        if(save.isChecked()){
            getSharedPreferences("checkInfo",0).edit().putBoolean("saveState",true).commit();
            //记录是否Checkbox状态
        }

        //Toast.makeText(LoginActivity.this, "成功", Toast.LENGTH_LONG).show();
    }
    private void getInfo() {
        SharedPreferences sp = getSharedPreferences("user.xml", MODE_PRIVATE);
        String name = sp.getString("username", "");
        String pass = sp.getString("password", "");
        username.setText(name);
        //password.setText(password);
        password.setText(pass);
        if(getSharedPreferences("checkInfo",0).getBoolean("saveState",false)){
            save.setChecked(true);
            // 验证上次是否勾选保存密码，如果保存则再保存
        }
//CheckBox默认状态为false，如果上次没有选中复选框，全部默认就好

    }

    private void initWindow() {//初始化，将状态栏和标题栏设为透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
    /*
    //用户根据点击事件来找到相应的功能
    public void fun(View v){
        switch (v.getId()){
            case R.id.login_register:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String n = username.getText().toString().trim();
                        String psw = password.getText().toString().trim();
                        UserDao ud = new UserDao();
                        boolean result =ud.register(n,psw);
                        if (!result){
                            Looper.prepare();
                            Toast toast = Toast.makeText(LoginActivity.this,"注册成功！",Toast.LENGTH_SHORT);
                            toast.show();
                            Looper.loop();
                        }
                        Log.i(TAG,"fun"+result);

                        //以上为jdbc注册
                    }
                }).start();
                break;
            case R.id.login:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String n = username.getText().toString().trim();
                        String psw = password.getText().toString().trim();
                        if (n.equals("")||psw.equals("")){
                            Looper.prepare();
                            Toast toast = Toast.makeText(LoginActivity.this,"输入不能为空！",Toast.LENGTH_SHORT);
                            toast.show();
                            Looper.loop();
                        }
                        UserDao ud = new UserDao();
                        Boolean result = ud.login(n,psw);
                        if (!result){
                            Looper.prepare();
                            Toast toast=Toast.makeText(LoginActivity.this,"用户名不存在或密码错误！",Toast.LENGTH_SHORT);
                            toast.show();
                            Looper.loop();
                        }else{
                            Looper.prepare();
                            Toast toast=Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT);
                            toast.show();

                            //一下代码为跳转界面
                            // Intent intent=new Intent(MainActivity.this,info.class);
                            //intent.putExtra("name",n);
                            // startActivity(intent);
                            Looper.loop();

                        }

                        //以上为jdbc登录
                    }
                }).start();

        }

    }*/
}
